﻿using ExpenseSystem.Core.CustomerModules;
using ExpenseSystem.Core.DataAddOn;
using ExpenseSystem.Core.OrderModules;
using ExpenseSystem.Fether;
using ExpenseSystem.Model;
using ExpenseSystem.Common;

namespace ExpenseSystem.Core.BonusModules
{
    public class DesignerBonusUpdCusAndOrdManager:DesignerBonusManager
    {
        DesignerBonusManager orgDbManager;
        CustomerManager customerManager = new CustomerBaseManager();
        OrderManager orderManager = new OrderBaseManager();
        public DesignerBonusUpdCusAndOrdManager(DesignerBonusManager designerBonusManager)
        {
            this.orgDbManager = designerBonusManager;
        }
        private bool UpdateCustomer(long site, long cusId, decimal amount, out string message)
        {
            bool result = false;
            CustomerFetcher customerFetcher = new CustomerFetcher();
            CustomerModel customerInfo = customerFetcher.Get(site, cusId, out message);
            if (customerInfo == null)
            {
                LogHelper.Error(this.GetType().FullName,"无法找到对应的客户信息");
                return false;
            }
            customerInfo.DesignerBonus += amount;
            result = customerManager.Save(customerInfo, out message);
            if(result)
            {
                LogHelper.Info(this.GetType().FullName, "更新客户累计设计费用信息成功");
            }
            else
            {
                LogHelper.Error(this.GetType().FullName, string.Format("更新客户累计设计费用信息失败:{0}",message));
            }
            return result;
        }
        private bool UpdateOrder(long site, long ordId, decimal amount, out string message)
        {
            if (ordId == 0)
            {
                message = "";
                return true;
            }
            bool result = false;
            OrderFetcher orderFetcher = new OrderFetcher();
            OrderModel orderModel = orderFetcher.Get(site, ordId, out message);
            if (orderModel == null)
            {
                LogHelper.Error(this.GetType().FullName, "无法找到对应的订单信息");
                return result;
            }
            orderModel.DesignerBonusPaid += amount;
            result = orderManager.Save(orderModel, out message);
            if (result)
            {
                LogHelper.Info(this.GetType().FullName, "更新订单的累计设计费用信息成功");
            }
            else
            {
                LogHelper.Error(this.GetType().FullName, string.Format("更新订单的累计设计费用信息失败:{0}", message));
            }
            return result;
        }

        public override bool Approved(long id, out string message)
        {
            DesignerBonusFetcher dbFetcher = new DesignerBonusFetcher();
            DesignerBonusModel dbModel;
            decimal amount = 0;
            if (id == 0)
            {
                message = "传入的参数有误";
                return false;
            }
            else
            {
                dbModel = dbFetcher.Get(profile.SiteId, id, out message);
                if (dbModel == null)
                {
                    return false;
                }
                amount += dbModel.Bonus;
            }
            bool result = orgDbManager.Approved(id, out message);
            if (result)
            {
                result = UpdateCustomer(profile.SiteId, dbModel.CustomerId, amount, out message);
            }
            if (result&&dbModel.OrderId>0)
            {
                result = UpdateOrder(profile.SiteId, dbModel.OrderId, amount, out message);
            }
            return result;
        }
        public override bool Revert(long modelid, out string message)
        {
            DesignerBonusFetcher dbFetcher = new DesignerBonusFetcher();
            DesignerBonusModel dbModel;
            decimal amount = 0;
            if (modelid == 0)
            {
                message = "传入的参数有误";
                return false;
            }
            else
            {
                dbModel = dbFetcher.Get(profile.SiteId, modelid, out message);
                if (dbModel == null)
                    return false;
                amount -= dbModel.Bonus;
            }
            bool result = orgDbManager.Revert(modelid, out message);
            if (result)
            {
                result = UpdateCustomer(profile.SiteId, dbModel.CustomerId, amount, out message);
            }
            if (result&&dbModel.OrderId>0)
            {
                result = UpdateOrder(profile.SiteId, dbModel.OrderId, amount, out message);
            }
            return result;
        }


        public override bool Delete(DesignerBonusModel dbm, out string message)
        {
                return orgDbManager.Delete(dbm, out message);

        }

        public override bool DeleteById(long[] PayId, out string message)
        {
            return orgDbManager.DeleteById(PayId, out message);
        }

        public override bool Save(DesignerBonusModel dbmodel, out string message, params IAddable<DesignerBonusModel>[] addables)
        {
            return orgDbManager.Save(dbmodel, out message, addables);
        }
    }
}
